package com.cainli.szbus.ui;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.cainli.szbus.R;
import com.cainli.szbus.data.model.BusLine;
import com.cainli.szbus.data.model.BusStat;
import com.cainli.szbus.data.requestmanager.RequestFactory;
import com.cainli.szbus.log.CLog;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.requestmanager.RequestManager.RequestListener;

/**
 * 
 * @author cainli 
 * 2013-4-19 下午1:11:28
 */
public class MainActivity extends BaseActivity implements RequestListener ,OnClickListener{
	private static final String TAG = MainActivity.class.getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.main);
		bindViews();
	}

	private void bindViews() {
		findViewById(R.id.test_btn).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		for (int i = 0; i < mRequestList.size(); i++) {
			Request request = mRequestList.get(i);

			if (mRequestManager.isRequestInProgress(request)) {
				mRequestManager.addRequestListener(this, request);
				setProgressBarIndeterminateVisibility(true);
			} else {
				mRequestManager.callListenerWithCachedData(this, request);
				i--;
				mRequestList.remove(request);
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (!mRequestList.isEmpty()) {
			mRequestManager.removeRequestListener(this);
		}
	}

	@Override
	public void onRequestFinished(Request request, Bundle resultData) {
		if (mRequestList.contains(request)) {
			setProgressBarIndeterminateVisibility(false);
			mRequestList.remove(request);
			switch (request.getRequestType()) {
				case RequestFactory.REQUEST_TYPE_BUSLINE_LIST:
					ArrayList<BusLine> buslineList = resultData
					.getParcelableArrayList(RequestFactory.BUNDLE_EXTRA_BUSLINE_LIST);
					for (BusLine bl : buslineList) {
						CLog.v(TAG, buslineList.toString());
					}
					break;
				case RequestFactory.REQUEST_TYPE_BUSSTATS_LIST:
					ArrayList<BusStat> busStatList = resultData
					.getParcelableArrayList(RequestFactory.BUNDLE_EXTRA_BUSSTATS_LIST);
					for (BusStat bl : busStatList) {
						CLog.v(TAG, busStatList.toString());
					}
					break;
			default:
				break;
			}
		}
	}

	@Override
	public void onRequestConnectionError(Request request, int statusCode) {
		if (mRequestList.contains(request)) {
			setProgressBarIndeterminateVisibility(false);
			mRequestList.remove(request);
		}
	}

	@Override
	public void onRequestDataError(Request request) {
		if (mRequestList.contains(request)) {
			setProgressBarIndeterminateVisibility(false);
			mRequestList.remove(request);
		}
	}

	@Override
	public void onRequestCustomError(Request request, Bundle resultData) {
		// Never called.
	}

	@Override
	public void onClick(View v) {
		setProgressBarIndeterminateVisibility(true);
//		Request request = RequestFactory.getBusLineListRequest(
//			Uri.encode("深圳"), "204", 0);
//		mRequestManager.execute(request, this);
//		mRequestList.add(request);
		Request request2 = RequestFactory.getBusStatListRequest(
				Uri.encode("深圳"), Uri.encode("华联花园"));
		mRequestManager.execute(request2, this);
		mRequestList.add(request2);
	}

}
